package pdm.h_2011_01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;

class addListener implements View.OnClickListener
{
	private Intent responseIntent_;
	private Activity activity_;
	private EditText et_;
	
	public addListener(Activity activity, EditText et)
	{
		activity_     = activity;
		et_           = et;
		
		responseIntent_ = new Intent();
	}
	
	public void onClick(View v) 
	{
		Log.println (Log.DEBUG, "buton", "Add");
		
		String str = et_.getText().toString();
		
		if (str.length() != 0 && str.matches("^([a-zA-Z]+)(([ ,-])([a-zA-Z]+))*$") ) {
		
		responseIntent_.putExtra ("city", str);


		// 1 - pentru OK
		// 0 - pentru Cancel
		activity_.setResult (1, responseIntent_);
		activity_.finish();
		}
	}
}

class cancelListener implements View.OnClickListener
{
	private Activity activity_;
	
	public cancelListener(Activity activity)
	{
		activity_     = activity;
	}
	
	public void onClick(View v) {
		Log.println (Log.DEBUG, "buton", "Cancel");
		
		// 1 - pentru OK
		// 0 - pentru Cancel
		activity_.setResult (0);
		
		activity_.finish();
	}
}

public class AddCity extends Activity
{
	Button addButton_;
	Button cancelButton_;
	EditText et_;
	
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_city_dialog); 
        
		Log.println (Log.DEBUG, "activity", "Start Add city dialog");        
        
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width  = LayoutParams.FILL_PARENT;
        params.height = LayoutParams.WRAP_CONTENT; 
        getWindow().setAttributes(params);
        
        et_ = (EditText) findViewById(R.id.edittext);
        
        addButton_ = (Button) findViewById(R.id.addButton);
        addButton_.setOnClickListener(new addListener(this, et_));
        
        cancelButton_ = (Button) findViewById(R.id.cancelButton);
        cancelButton_.setOnClickListener(new cancelListener(this));
    }
}
